<!--图片上传控件，改造自mpvue-weui-->
<template>
  <div class="weui-uploader">
    <div class="weui-uploader__bd">
      <div class="weui-uploader__files" id="uploaderFiles">
        <div v-for="(item, index) in uploadImg" :key="index" class="weui-uploader__file--box">
          <div class="weui-uploader__file">
            <image class="weui-uploader__img" :src="item" mode="aspectFill" @click.stop.prevent="previewImage(item, $event)" :id="item" />
            <div class="delete-icon" @click.stop.prevent="deleteImg" :id="item" :data-index="index" :key="index"></div>
          </div>
        </div>
        <div v-if="uploadImg.length < count">
          <div class="upload-style-2 font-10"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        uploaderIs: true,
        delIconShow: false
      }
    },
    props: {
      imgSrc: {
        type: Array,
        default: function() {
          return []
        }
      },
      count: {
        type: Number,
        default: 9
      }
    },
    onLoad() {
      // 初始化data
      Object.assign(this.$data, this.$options.data())
    },
    mounted() {
    },
    computed: {
      uploadImg() {
        return this.imgSrc
      }
    },
    methods: {
      chooseImage(e) {
        this.uploaderIs = false
        this.$pt.chooseImage({
          count: this.count, // 默认9
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: res => {
            this.$emit('upLoadStoreSuccess', res)
          },
          fail: err => {
            this.uploaderIs = true
            this.$emit('upLoadFail', err)
          },
          complete: res => {
            this.$emit('upLoadComplete', res)
          }
        })
      },
      previewImage(item, e) {
        this.$pt.previewImage({
          current: e.currentTarget.id, // 当前显示图片的http链接
          urls: item // 需要预览的图片http链接列表
        })
      },
      deleteImg(e) {
        Array.prototype.indexOf = function(val) { // eslint-disable-line
          for (let i = 0; i < this.length; i++) {
            if (this[i] === val) return i
          }
          return -1
        }
        Array.prototype.remove = function(val) { // eslint-disable-line
          let index = this.indexOf(val)
          if (index > -1) {
            this.splice(index, 1)
          }
        }
        this.uploadImg.remove(e.currentTarget.id)
        if (!this.uploadImg.length) {
          this.uploaderIs = true
        }
        let uploadDeleteObj = {
          src: e.currentTarget.id,
          index: e.currentTarget.dataset.index,
          files: this.uploadImg
        }
        this.$emit('uploadStoreDelete', uploadDeleteObj)
      }
    }
  }
</script>

<style lang="less" scoped>
  .weui-uploader__files{
    zoom: 1;
    display: flex;
    &::after{
      content: '';
      clear: both;
      display: block;
    }
  }
  .weui-uploader__file--box{
    &:last-child{
      .weui-uploader__file{
        margin-right: unset;
      }
    }
  }
  .weui-uploader__file {
    position: relative;
  }
  .weui-uploader__bd {
    overflow: visible;
  }
  .upload-style{
    width: 79px;
    height: 79px;
    box-sizing: border-box;
    border: 1px dashed #A9A9A9;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    line-height: 1.2;
    color: #ABABAB;
    margin-bottom: 9px;
  }
  .upload-style-2{
    width: 66px;
    height: 66px;
    background-color: #F8F8F8;
    position: relative;
    margin-bottom: 9px;
    &::before{
      content: '';
      position: absolute;
      width: 14px;
      height: 2px;
      background-color: #999999;
      left: 26px;
      top: 32px;
    }
    &::after{
      content: '';
      position: absolute;
      width: 2px;
      height: 14px;
      background-color: #999999;
      left: 32px;
      top: 26px;
    }
  }
  .weui-uploader__img-2{
    width: 66px;
    height: 66px;
  }
  .delete-icon {
    position: absolute;
    width: 20px;
    height: 20px;
    background: #f43530;
    right: 0;
    top: -10px;
    border-radius: 20px;
    z-index: 5;
  }
  .delete-icon::before {
    content: '';
    width: 13px;
    height: 2px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
  }
</style>
